package com.test.router.system.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class CommonUtils {

    private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    public static int randomInt(int min, int max) {
        return randomInt(max) % (max - min + 1) + min;
    }

    public static int randomInt(int max) {
        Random random = new Random();
        return random.nextInt(max);
    }

    public static String numberFormat(double m, String patten) {
        if (patten == null) {
            patten = "#0.00";
        }
        DecimalFormat format = new DecimalFormat(patten);
        return format.format(m);
    }

    public static boolean testAlpha(String str) {
        String regx = "^\\w+$";
        return str.matches(regx);
    }

    public static boolean testNumber(String str) {
        if (str == null) {
            return false;
        }
        String regx = "^[\\-0-9][0-9]*(.[0-9]+)?$";
        return str.matches(regx);
    }

    public static boolean testMobile(String mobile) {
        if (mobile == null) {
            return false;
        }
        String regx = "^1[3|4|5|6|7|8|9]\\d{1}\\d{8}$";
        return mobile.matches(regx);
    }

    public static boolean testEmail(String mail) {
        String regx = "^([a-z0-9A-Z]+[-|\\\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\\\.)+[a-zA-Z]{2,}$";
        return mail.matches(regx);
    }

    /**
     * 是否为16进制
     *
     * @param hex
     * @return
     */
    public static boolean isHex(String hex) {
        String regex = "^[A-Fa-f0-9]+$";
        if (hex.matches(regex)) {
            return true;
        }
        return false;
    }

    public static String makeMessageId() {
        return UUID.randomUUID().toString();
    }

    public static String getClientIp(HttpServletRequest request) {

        if (request == null) {
            return "0.0.0.0";
        }

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }

    public static String listToString(List<Object> list, String split) {
        StringBuffer sb = new StringBuffer();
        if (list != null) {
            for (Object o : list) {
                sb.append(o.toString()).append(split);
            }
            if (sb.length() > 0 && sb.lastIndexOf(split) != -1) {
                return sb.substring(0, sb.length() - 1);
            }
        }
        return sb.toString();
    }


}
